#include <stdio.h>
#include <stdlib.h>
#include <pic18f6722.h>

#include "App_PowerStapler.h"
#define BOOST_WORK TRISC
#define WORK_LED_TIME 1500
#define REDUCE_LOW_POWER_TIME 20
typedef enum{
 CHARGE_FULL,
 CHARGE_ING,
 DISCHARGE,
}CHARGE_STATE_T;
CHARGE_STATE_T charge_event=DISCHARGE;
void main() 
{
    unsigned int Battery_Voltage;
    unsigned int Battery_mA,Battery_mA_temp = 0;
    unsigned int Battery_pre_mA = 0x7fff, Battery_Current_mA = 0x7fff;
    unsigned int  Battery_fullcharge_Pre_mA = 0x7fff;
    unsigned int Battery_fullcharge_Current_mA = 0x7fff;
    unsigned int Battery_discharge_Current_mA = 0x7fff;  
    unsigned int  Battery_discharge_Pre_mA = 0x7fff;
    unsigned int  Battery_Current_error_mA=0x7fff;
    unsigned int  Battery_Pre_error_mA=0x7fff;
    unsigned int charge_flag=0;
    int charge_counter = 0;
    int discharge_counter = 3;   
    PIC_Initialize();
    GPIO_Initialize();
    Device_Indicator_Initialize(0);
    Device_MotorInitialize();
    Device_LTC2943_Initialize();
    App_Interrupt_Initialize();
    App_PowerStapler_Initialize();
    LED_Flash = 0;
    unsigned int warning_flag=0;
    Charge_Status = 0;
    LED_Flash = 0;
    Device_Indicator_SetLED(0); //dark
    //HardWareTest(); 
    unsigned int full_charge_flag=0;
    unsigned int count_time=0;
    unsigned int charge_ack_flag=0;
    BOOST_WORK=BOOST_WORK&0x7F;
    unsigned char is_ok_flag=0;
    unsigned int voltage_battery=0;
    unsigned char is_normal_flag=0;
    unsigned char low_voltage_to_check_charge=0;     //1-????????????   0-??????
    uint time=0;
    unsigned int str[100]={0,};                     
     int index=0;
     set_yellow_led(0);

       Work_LED=0;
//       RE3 = 1;
    while (1)  
    {
     Work_LED=1;
     LED_Flash=0;
     warn_flag=0;  
     int x  = 0;
     work_led_flag=0;
     //unsigned char Device_PowerCheckALCC(void)
   
    time= get_time_count();
    if(time>200)
    {
        BOOST_WORK=BOOST_WORK&0x7F;
        Work_LED=1;
    }
     voltage_battery=Device_LTC2943_ReadVoltage();
     if(voltage_battery<=310)
     {
      
      //  Device_Indicator_SetLED(1); 
         if(time<WORK_LED_TIME && time>REDUCE_LOW_POWER_TIME)
         {
             set_green_led(0);
             set_red_led(1);     //red
         }
         else
         {
             set_yellow_led(0);
         }
         warn_flag=1;
       
        continue;        
     }
     else if(voltage_battery<330 && voltage_battery>310)
     {
         if(is_ok_flag==1)
         {
             continue;
         }
     }
     else
     {
         is_ok_flag=0;
     }
     if(timecouner_fullcharge >=10)
     {
        Battery_fullcharge_Pre_mA=Battery_fullcharge_Current_mA;
         Battery_fullcharge_Current_mA = Device_LTC2943_ReadCurrent();
         str[index]=Battery_fullcharge_Current_mA;
         timecouner_fullcharge=0;
         if((Battery_fullcharge_Current_mA>=0xff00)&&(Battery_fullcharge_Current_mA<=0x01ff))
         {
            timecouner_charge = 0;
            timecouner_dicharge = 0;
            Battery_discharge_Current_mA=0x7fff;
            Battery_Current_mA=0xfff;
            Battery_Current_error_mA=0x7fff;
       
            Battery_fullcharge_Pre_mA = 0x7fff;
            Battery_fullcharge_Current_mA = 0x7fff;
            Device_LTC2943_Init_AccCharge();
            index=0;
         }
         if(index>=10)
         {
             int tem=0;
             tem=str[10]-str[0];
             if(charge_event==CHARGE_ING)
             {
                if(tem<=0x100 && tem >=0)
                {
                   charge_event=CHARGE_FULL;
                   Charge_Status = 3;        
                   LED_Flash = 0;
                   set_red_led(0);
                   set_green_led(1);
                   charge_ack_flag=0;
                }
             }
              index=0;
         }
         else
         index++;
//        if((Battery_fullcharge_Current_mA<(Battery_fullcharge_Pre_mA+0x05))&& (Battery_fullcharge_Current_mA>Battery_fullcharge_Pre_mA) && (Device_LTC2943_ReadVoltage()>420))   //check discharge
//        {
//             if(charge_ack_flag>=2)
//             {
//                Charge_Status = 3;         //charge full
//                LED_Flash = 0;
// //               Device_Indicator_SetLED(2); //Green
//                set_red_led(0);
//                set_green_led(1);
//                charge_ack_flag=0;
//             }
//             charge_ack_flag++;
//        }
//        else
//        {
//            charge_ack_flag=0;
//        }
     }
//     if(timecounter>=2)
//     {
//         Battery_Pre_error_mA=Battery_Current_error_mA;
//         Battery_Current_error_mA=Device_LTC2943_ReadCurrent();
//         timecounter=0;
//         int tem1_bat=Battery_Pre_error_mA - 0x2384;
//         int tem2_bat=Battery_Pre_error_mA + 0x2384;
//#if 0
//         if((Battery_Pre_error_mA < (Battery_Current_error_mA - 0x2384))||(Battery_Pre_error_mA>(Battery_Current_error_mA+0x2384)))
//         {
//            Device_Indicator_SetLED(1);     //red
//             warn_flag=1;
//             continue;    
//         }
//#endif
//
//
// 
//#if 1
//         if((Battery_Current_error_mA >= tem1_bat)&&(Battery_Current_error_mA <= tem2_bat)&&((Battery_Pre_error_mA<=0xdc7b)&&(Battery_Pre_error_mA>0x2384)))
//         {
//             warn_flag=0;
//         }
//         else
//         {
//            Device_Indicator_SetLED(1);     //red
//             warn_flag=1;
//             continue;  
//         }
//#endif
//     }
  

   
     
  //check charge status
     
     if(timecouner_dicharge >= 6)
     {
             Battery_discharge_Pre_mA  = Battery_discharge_Current_mA;
             Battery_discharge_Current_mA = Device_LTC2943_ReadCurrent();
             timecouner_dicharge = 0;
             timecouner_charge = 0;

              if(Battery_discharge_Current_mA <Battery_discharge_Pre_mA)   //check discharge
              {
                 Charge_Status = 0;         //charge status
                 LED_Flash = 0;
                 set_yellow_led(0); //dark
                 charge_event=DISCHARGE;
//                 if(low_voltage_to_check_charge!=1)

              }
     }
     
     if(charge_event!=CHARGE_FULL)
     if(timecouner_charge >= 4)
     {
             Battery_pre_mA = Battery_Current_mA;
             Battery_Current_mA = Device_LTC2943_ReadCurrent();
            // Device_LTC2943_Init_AccCharge();
            unsigned int tem1= Device_LTC2943_ReadVoltage();
             timecouner_charge = 0;
//                          str[index]=tem;
//            if(index>=100)
//                index=0;
//            else
//                index++;
//                          str1[index1]=Battery_Current_mA;
//                          if(index1>=100)
//                              index1=0;
//                          else
//                              index1++;
              if(Battery_Current_mA > Battery_pre_mA)  //check charge
              { 
                   Charge_Status = 1;
                   LED_Flash = 0;   
                   charge_event=CHARGE_ING;
                   set_yellow_led(1); //yellow
              }
     }
     
     
        
    
    //check ltc2942 , if error occurer , red led is on while 
     
     
     
     
     #if 0
     
         if( timecounter > 6)
        {
             Battery_mA_temp = Battery_mA;
             Battery_mA = Device_LTC2943_ReadCurrent();
             Device_LTC2943_Init_AccCharge();
             timecounter = 0;
             
           if(Battery_mA > Battery_mA_temp)
            {
               if(charge_counter < 2)  charge_counter ++;
               
               if(charge_counter >= 2)
               {
                   charge_counter = 2;
                   Charge_Status = 1;
                   LED_Flash = 0;
                   Device_Indicator_SetLED(3); //yellow
               }
             }
       //    else if(Battery_mA==Battery_mA_temp)
       //    {
       //         Charge_Status = 2;
       //        LED_Flash = 0;
       //         Device_Indicator_SetLED(2); //green
       //    }
            else
             { 
               if(charge_counter > 0)  charge_counter--;
               
               if(charge_counter == 0)
               {
                 Charge_Status = 0;
                 LED_Flash = 0;
                 Device_Indicator_SetLED(0); //dark
               }
             }
             
        }
  //*************************************************      
#endif  //
     //CHECK LOW VOLTAGE
////  //check voltage       
//         if( Charge_Status == 0)
//         {
//            Battery_Voltage = Device_LTC2943_ReadVoltage();
//             
//            if((Battery_Voltage <= 380) &&  (Battery_Voltage >= 350)  )    // low power 
//            {  
//        
//                if(get_time_count()<WORK_LED_TIME)
//                set_yellow_led(1);
//                else
//                set_yellow_led(0);
//               if(is_normal_flag==0)
//                   set_time_count(0);
//               is_normal_flag=1;
//               low_voltage_to_check_charge=1;
//               warn_flag=1;
//              // warning_flag=1;
//               LED_Flash = 1; // YELLOW FLASH  
//            }
//            else if(Battery_Voltage>380 && Battery_Voltage<390)
//            {
//                if(is_normal_flag==1)
//                {
//                    Charge_Status = 0;
//                    low_voltage_to_check_charge=1;
////                    set_yellow_led(1);
//                }
//            }
//            else
//            {
//                low_voltage_to_check_charge=0;
//                is_normal_flag=0;
//                Charge_Status = 0;
//                LED_Flash = 0;
//                set_yellow_led(0); //dark 
//            }
////            if( (Battery_Voltage > 380) )   // normal power
////            {
////                Charge_Status = 0;
////                LED_Flash = 0;
////                set_yellow_led(0); //dark
////            }
//         }
     //*****************************************************************************
     
     //check key
        Battery_Voltage = Device_LTC2943_ReadVoltage();
        if ((Device_Trigger_CheckFire() == 1) && (Device_Trigger_CheckSafe() == 1)) 
        {
//            Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticClosureCurrent);
//            Device_MotorOutControl(0);
            if(Charge_Status == 0)     set_yellow_led(0);
        }
        if (Battery_Voltage >= 310)
        {
            if (Device_Trigger_CheckFire() == 0) // key 1 is pressed
            {
                
                work_led_flag=0;
//                Work_LED=1;
                BOOST_WORK=BOOST_WORK|0x80;
                  for(int i=0;i<50;i++)
                    for(int j=0;j<10;j++);
                Phase_ElasticClosure();            //turn right 
                BOOST_WORK=BOOST_WORK&0x7F;
                Work_LED=1;
                set_time_count(0);
            }
            if (Device_Trigger_CheckSafe() == 0)   // key 2 is pressed
            {
                work_led_flag=0;
//                Work_LED=1;
                BOOST_WORK=BOOST_WORK|0x80;
                for(int i=0;i<50;i++)
                    for(int j=0;j<10;j++);
                Phase_ElasticOpen();               //turn left
                BOOST_WORK=BOOST_WORK&0x7F;
                Work_LED=1;
                set_time_count(0);
            }
        }
    }
}















//********************************************************************
/* Initialize PowerStapler
 * input:  void
 * return: void
 * author: Guichuan.He
 * date: 2016-9-29
*/
//**********************************************************************
void App_PowerStapler_Initialize()
{
     Logic_Initialize_SetDataDefault();
}
//********************************************************************
/* hardwere test
 * input:  void
 * return: void
 * author: Guichuan.He
 * date: 2016-9-29
*/
//**********************************************************************
void HardWareTest()
{
 
    unsigned int i,j,k,l;
      Device_MotorSetCurrent(0xff);
 
    while(1)
    { 
       if (Device_Trigger_CheckFire() == 0) 
        {
             Device_MotorOutControl(1);
        }

        else 
            
          Device_MotorOutControl(0);
       while(1)
       {
            if (Device_Trigger_CheckFire() == 0)
                break;
       }
    }
      

      

     
    while(1)
    {
    i = Device_LTC2943_ReadVoltage();
    }
    while (1) 
    {
        if (Device_Trigger_CheckFire() == 0) 
        {

            Device_MotorOutControl(1);
            PowerStaplerData.MotorControl.Turns_Number = 10;
         
        }
        else 
        {
            //   Device_Indicator_SetCountLED(1);
            //   Device_Indicator_SetBatteryLED(1);
            Device_MotorOutControl(0);
        }

    }
    
    
 }
    
    

   
  
    
    
    
    
    

  
    

 

  
  
  